package queue;

import java.util.concurrent.LinkedBlockingQueue;

public class LinkedBlockQueueDemo {
    /*
           LinkedBlockingQueue :
                描述： 阻塞队列，链表实现
                特点：
                   1. 底层是链表
                   2. 可以是有界,也可以无界【容量Integer.MAX_VALUE】
                   3. 不能存null
     */
    public static void main(String[] args) {
        LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        queue.put(i);
                        System.out.println(Thread.currentThread().getName()+"生产了"+i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                }
            }
        },"小同志");

        t1.start();


        Thread t2 = new Thread(new Runnable() {


            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(8);
                        Integer take = queue.take();
                        System.out.println("小同志正在消费" + take);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"老同志");
        t2.start();
    }
}
